package org.larkdoc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.larkdoc.dao.DocInfoMapper;
import org.larkdoc.model.bo.DocBO;
import org.larkdoc.model.entity.DocInfo;
import org.larkdoc.model.entity.DocRelProject;
import org.larkdoc.model.entity.ProjectInfo;
import org.larkdoc.model.vo.DocVO;
import org.larkdoc.model.vo.PageVO;
import org.larkdoc.service.DocRelProjectService;
import org.larkdoc.service.DocService;
import org.larkdoc.service.ProjectService;
import org.larkdoc.util.DozerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 文档业务功能实现类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service("docService")
public class DocServiceImpl implements DocService {
    
    @Autowired
    DocInfoMapper        docInfoMapper;
    
    @Autowired
    DocRelProjectService docRelProjectService;
    
    @Autowired
    ProjectService       projectService;
    
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return docInfoMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public int insert(DocInfo record) {
        return docInfoMapper.insert(record);
    }
    
    @Override
    public int insertSelective(DocInfo record) {
        return docInfoMapper.insertSelective(record);
    }
    
    @Override
    public DocInfo selectByPrimaryKey(Integer id) {
        return docInfoMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public int updateByPrimaryKeySelective(DocInfo record) {
        return docInfoMapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int updateByPrimaryKey(DocInfo record) {
        return docInfoMapper.updateByPrimaryKey(record);
    }
    
    @Override
    public List<DocInfo> selectByDocInfo(DocInfo docInfo) {
        return docInfoMapper.selectByDocInfo(docInfo);
    }
    
    @Override
    public DocInfo getByIdAndAccount(Integer id, String account) {
        return docInfoMapper.getByIdAndAccount(id, account);
    }
    
    @Override
    public PageInfo<DocInfo> page(PageVO<DocVO> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DocInfo> list = selectByDocVO(page.getSearch());
        return new PageInfo<DocInfo>(list);
    }
    
    @Override
    public int insertDocBo(DocBO docBO) {
        int result = 0;
        if (null != docBO) {
            DocInfo docInfo = DozerUtil.conversion(docBO, DocInfo.class);
            result = insertSelective(docInfo);
            if (result == 1) {
                List<DocRelProject> projects = docBO.getProjectRelDocs();
                if (CollectionUtils.isNotEmpty(projects)) {
                    for (DocRelProject projectRelDoc : projects) {
                        projectRelDoc.setDocId(docInfo.getId());
                    }
                    result = docRelProjectService.insertBatch(projects);
                }
            }
        }
        return result;
    }
    
    @Override
    public PageInfo<DocBO> pageBO(PageVO<DocVO> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DocBO> list = docInfoMapper.queryByDocVO(page.getSearch());
        return new PageInfo<DocBO>(list);
    }
    
    @Override
    public List<DocInfo> selectByDocVO(DocVO docVO) {
        if (null != docVO) {
            return docInfoMapper.selectByDocVO(docVO);
        } else {
            return null;
        }
    }
    
    @Override
    public boolean checkExists(DocInfo docInfo,String account) {
        if (null == docInfo) {
            return true;
        }
        docInfo.setCreator(account);
        return docInfoMapper.checkExists(docInfo) > 0 ? true : false;
    }
    
    @SuppressWarnings("unchecked")
    public PageInfo<DocBO> pageBOBak(PageVO<DocVO> page) {
        PageInfo<DocBO> result = null;
        PageInfo<DocInfo> docPage = page(page);
        if (null != docPage) {
            result = DozerUtil.conversion(docPage, PageInfo.class);
            List<DocInfo> docInfos = docPage.getList();
            // TOTO 可改成使用mybatis组合对象查询返回。
            if (CollectionUtils.isNotEmpty(docInfos)) {
                List<DocBO> boList = new ArrayList<DocBO>(docInfos.size());
                DocBO boTemp = null;
                List<Integer> docIds = new ArrayList<Integer>();
                for (DocInfo docInfo : docInfos) {
                    if (null == docInfo) {
                        continue;
                    }
                    boTemp = new DocBO();
                    //                    boTemp.setDocInfo(docInfo);
                    boList.add(boTemp);
                    docIds.add(docInfo.getId());
                }
                // 获取关系表
                List<DocRelProject> rel = docRelProjectService.selectByDocIds(docIds);
                if (CollectionUtils.isNotEmpty(rel)) {
                    // 
                    List<ProjectInfo> projects = projectService.selectByDocIds(docIds);
                    Map<Integer, List<ProjectInfo>> tempMap = new HashMap<Integer, List<ProjectInfo>>(rel.size());
                    List<ProjectInfo> proTemp = null;
                    for (DocRelProject relTemp : rel) {
                        if (null == relTemp) {
                            continue;
                        }
                        proTemp = tempMap.get(relTemp.getDocId());
                        proTemp = proTemp == null ? new ArrayList<ProjectInfo>() : proTemp;
                        for (ProjectInfo projectInfo : projects) {
                            if (null == projectInfo) {
                                continue;
                            }
                            if (relTemp.getProjectId() == projectInfo.getId()) {
                                proTemp.add(projectInfo);
                                break;
                            }
                        }
                        tempMap.put(relTemp.getDocId(), proTemp);
                    }
                    // 组合进BO对象
                    /*for (DocBO docBOTemp : boList) {
                        if (null == docBOTemp || null == docBOTemp.getDocInfo()) {
                            continue;
                        }
                        docBOTemp.setProjects(tempMap.get(docBOTemp.getDocInfo().getId()));
                    }*/
                }
                result.setList(boList);
            }
        }
        return result;
    }
    
    @Override
    public DocBO getAuthDocById(Integer id, String account) {
        if (id > 0 && StringUtils.isNotBlank(account)) {
            return docInfoMapper.getAuthDocById(id, account);
        } else {
            return null; 
        }
    }

    @Override
    public int deleteByIdsAndAccount(int[] ids, String account) {
        return docInfoMapper.deleteByIdsAndAccount(ids, account);
    }
    
    @Override
    public PageInfo<DocBO> pageByProject(PageVO<DocVO> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if(page.getSearch() != null && page.getSearch().getProjectIdList() != null 
            && page.getSearch().getProjectIdList().size() == 0) {
            page.getSearch().setProjectIdList(null);
        }
        List<DocBO> list = docInfoMapper.queryByProject(page.getSearch());
        return new PageInfo<DocBO>(list);
    }
}
